package com.pagoda.nerp.trade.app.message.web;

import com.pagoda.nerp.trade.app.message.dto.MdMessageDto;
import com.pagoda.nerp.trade.app.message.util.ParamValidUtil;
import com.pagoda.nerp.trade.common.BaseResult;
import com.pagoda.nerp.trade.common.GridDataResult;
import com.pagoda.nerp.trade.common.PageListResult;
import com.pagoda.nerp.trade.common.enums.ErrorCodeMappingEnums;
import com.pagoda.nerp.trade.service.MdMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息模块
 * */
@Controller
@RequestMapping("/message")
public class MdMessageCtr {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MdMessageService msgService;

    /**
     * 我的消息个数
     * @param params 供应商Id
     * */
    @RequestMapping(value = "/queryMsgCount", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<Object> queryMsgCount(@RequestBody Map<String,Object> params){
        logger.debug("请求 我的消息个数！ 供应商Id：{} ", params);

        try {
            // 1.参数校验
            String validMsg = ParamValidUtil.queryMsgValid(params);

            if ( null != validMsg){
                logger.error("请求参数缺失，缺失信息:{}", validMsg);
                return new BaseResult<Object>(ErrorCodeMappingEnums.ERROR_INVALID_PARAMS, validMsg);
            }

            // 2.查询消息个数
            Map<String,Object> result = msgService.queryMsgCount(params);

            return new BaseResult<Object>(ErrorCodeMappingEnums.SUCCESS, result);
        } catch (Exception e){
            logger.error("查询我的消息个数失败！", e);
            return new BaseResult<Object>(ErrorCodeMappingEnums.ERROR, e.getMessage());
        }

    }

    /**
     * 我的消息列表
     * @param params 供应商Id
     * */
    @RequestMapping(value = "/queryMsgList", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<Object> queryMsgList(@RequestBody Map<String,Object> params){
        logger.debug("请求 我的消息列表！ 供应商Id：{} ", params);

        try {
            // 1.参数校验
            String validMsg = ParamValidUtil.queryMsgValid(params);

            if ( null != validMsg){
                logger.error("请求参数缺失，缺失信息:{}", validMsg);
                return new BaseResult<Object>(ErrorCodeMappingEnums.ERROR_INVALID_PARAMS, validMsg);
            }

            // 2.查询消息列表
            PageListResult<MdMessageDto> pageList = msgService.queryMsgList(params);

            return new GridDataResult<List>(ErrorCodeMappingEnums.SUCCESS, pageList.getList(), pageList.getPageTotalCount());
        } catch (Exception e){
            logger.error("查询我的消息列表失败！", e);
            return new BaseResult<Object>(ErrorCodeMappingEnums.ERROR, e.getMessage());
        }

    }

}
